package be.sc.classicnetwork.service.impl;

import java.io.IOException;
import java.util.Map;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import be.sc.classicnetwork.exception.ApplicationException;
import be.sc.classicnetwork.service.MailService;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
 * MockMailServiceImpl.
 */
public class MockMailServiceImpl implements MailService {

    private Configuration configuration;

    /**
     * setFreeMarkerConfigurer.
     */
    public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
	this.configuration = freeMarkerConfigurer.getConfiguration();
    }

    /**
     * @see be.sc.classicnetwork.service.MailService#send(java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String,
     *      java.lang.String, java.util.Collection)
     */
    public void send(String from, String fromPersonal, String to, String subject, String text,
	    Map<String, byte[]> inlineAttachs) {
	System.out.println("Sending mail to " + to);
    }

    /**
     * @see be.sc.classicnetwork.service.MailService#sendFreeMarkerTemplate(java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String,
     *      java.util.Collection, java.lang.String, java.lang.Object)
     */
    public void sendFreeMarkerTemplate(String from, String fromPersonal, String to, String subject,
	    Map<String, byte[]> inlineAttachs, String template, Object model) {
	System.out.println("Sending mail to " + to);

	// generate text from model
	try {
	    String text = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(template), model);
	    System.out.println(text);
	} catch (IOException e) {
	    throw new ApplicationException(e);
	} catch (TemplateException e) {
	    throw new ApplicationException(e);
	}
    }

    public void setMailSender(JavaMailSender mailSender) {
    }

}
